package com.atguigu.redis_springboot.base;

import org.junit.Test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.Transaction;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * redis普通连接操作
 */
public class JedisTest {

    public static void main(String[] args) {
        // 创建jedis对象
        Jedis jedis = new Jedis("192.168.200.11",6379);
        // 验证密钥
        jedis.auth("123");
        // 测试，需要关闭防火墙
        // 临时关闭防火墙 systemctl stop firewalld
        String ping = jedis.ping();
        System.out.println(ping);
    }

    public Jedis init(){
        Jedis jedis = new Jedis("192.168.200.11",6379);
        jedis.auth("123");
        jedis.flushDB();
        return jedis;
    }

    /**
     * 对Key 的操作
     */
    @Test
    public void demo1(){
        Jedis jedis = init();

        // 添加key val
        jedis.set("k1", "v1");
        jedis.set("k2", "v2");
        jedis.set("k3", "v3");
        // 遍历keys
        Set<String> keys = jedis.keys("*");
        System.out.println(keys.size());
        for (String key : keys) {
            System.out.println(key);
        }
        // 检查Key是否存在
        System.out.println(jedis.exists("k1"));
        // ttl key 查看还有多少秒过期，-1表示永不过期，-2表示已过期。
        System.out.println(jedis.ttl("k1"));
        // 获取某个Key对应的值
        System.out.println(jedis.get("k1"));
        jedis.close();
    }

    /**
     * 对String的操作
     */
    @Test
    public void demo2(){
        Jedis jedis = init();
        jedis.mset("k1","v1","k2","v2","k3","v3");
        List<String> res = jedis.mget("k1", "k2", "k3");
        System.out.println(res);
        jedis.close();

    }

    /**
     * 对List的操作
     */
    @Test
    public void demo3(){
        Jedis jedis = init();
        jedis.lpush("user1","a","b","c");
        List<String> list = jedis.lrange("user1",0,-1);
        for (String element : list) {
            System.out.println(element);
        }
        jedis.close();
    }

    /**
     * 测试 set
     */
    @Test
    public void demo4(){
        Jedis jedis = init();
        jedis.sadd("user1","a","b","c","c");
        Set<String> keys = jedis.smembers("user1");
        for (String key : keys) {
            System.out.println(key);
        }
        jedis.close();
    }
    /**
     * 测试 hash
     */
    @Test
    public void demo5(){
        Jedis jedis = init();
        jedis.hset("user1","k1","v1");
        String hget = jedis.hget("user1", "k1");
        System.out.println(hget);

        Map<String,String> map = new HashMap<String, String>();
        map.put("k2","v2");
        map.put("k3","v3");
        map.put("k4","v4");
        jedis.hmset("user1",map);
        List<String> res = jedis.hmget("user1", "k1", "k2");
        for (String e:res){
            System.out.println(e);
        }
        jedis.close();
    }

    /**
     * 测试 有序集合Zset
     */
    @Test
    public void demo6(){
        Jedis jedis = init();
        jedis.zadd("user1",100,"u1");
        jedis.zadd("user1",200,"u2");
        jedis.zadd("user1",300,"u3");
        jedis.zadd("user1",400,"u4");
        jedis.zadd("user1",500,"u5");

        Set<String> zrange = jedis.zrange("user1", 0, -1);
        for (String e : zrange) {
            System.out.println(e);
        }
        jedis.close();
    }

    /**
     * 事务演示
     */
    @Test
    public void demo7(){
        Jedis jedis = init();
        Transaction transaction = jedis.multi();
        transaction.set("k1","v1");
        transaction.set("k2","v2");
//        transaction.discard();
        transaction.exec();
        jedis.close();


    }

}
